﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
    public class Animation
    {
        const int MAXFRAMES = 100;

	    float mFrameWidth;
	    float mFrameHeigth;
        int mHframe;
        int mVframe;
   	
	    int mCurrentFrame;
	    int mCantFrames;
        public String Name;

	    int[] mFrames= new int[MAXFRAMES];
	    Vector2[] mFramesPositions = new Vector2[MAXFRAMES];

        
        public Animation(String name, int txheight, int txwidth, int hframe, int vframe, int[] frames)
        {
 	        //memcpy(mFrames, frames, sizeof(int)*MAXFRAMES);
            Name = name;

            mFrames = frames;
            mHframe = hframe;
            mVframe = vframe;

	        mFrameWidth = txwidth/hframe;
	        mFrameHeigth = txheight/vframe;

  	        for (int i =0; i < mFrames.Length;i++){
		        mFramesPositions[i].X = (int) (mFrames[i])%hframe * mFrameWidth;
		        mFramesPositions[i].Y = (int) (mFrames[i])/hframe * mFrameHeigth;
	        }
	        mCurrentFrame=0;
            mCantFrames = mFrames.Length;
       }

       public void nextFrame(){
	        if (mCurrentFrame==mCantFrames-1) {
		        mCurrentFrame=0;
	        } else {
		        mCurrentFrame++;
	        }
       }

 //      public Rectangle getFrameRect()
 //      {
           
 //      }

        public Vector2 getUVCoordinates(int vertex){
	        Vector2 temp;
	        temp = mFramesPositions[mCurrentFrame];
	        //temp.v = 1 - temp.v;
	        if (vertex ==1) {
		        temp.Y+= mFrameHeigth;
	        }
	        if (vertex ==2) {
		        temp.X += mFrameWidth;
	        }
	        if (vertex ==3) {
		        temp.X += mFrameWidth;
		        temp.Y += mFrameHeigth;
	        }
	        return temp;
        }

        public Rectangle GetUVRectangle()
        {
            Vector2 temp;
            temp = mFramesPositions[mCurrentFrame];
            Rectangle r = new Rectangle((int)temp.X + (mCurrentFrame % mHframe), (int)temp.Y + (mCurrentFrame / mHframe), (int)mFrameWidth, (int)mFrameHeigth);
            return r;
        }



    }
}
